#include<vector>
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
    std::vector<std::vector<int>> res;
    std::vector<int> sort; 
    std::vector<TreeNode*> mid;
    int widthOfBinaryTree(TreeNode* root) {
        if (root == nullptr) return {};
        int l,r ;
        r=0;
        l=0;
        mid.push_back(root);
        sort.push_back(0);
        r++;
        int floor = 0;
        int maxres=0;
        res.push_back({});
        while (l!=r)
        {
            int size = r-l;
            res.push_back({});
          
            int min = 0;
            int max = 0;
            int flag = 0;
            while (size-->0)
            {
                res[floor].push_back(mid[l]->val);
                if (mid[l]->left == nullptr )
                {
                  
                }else
                {
                    mid.push_back(mid[l]->left);
                    if (flag==0)
                    {
                        min = sort[l]*2;
                        flag++;
                    }else
                    {
                        max = sort[l]*2;
                    }
                    sort.push_back(sort[l]*2-min);
                  
                    r++;
                }
                if (mid[l]->right == nullptr )
                {
                   
                }else
                {
                    mid.push_back(mid[l]->right);
                    if (flag==0)
                    {
                        min = sort[l]*2+1;
                        flag++;
                    }else
                    {
                        max = sort[l]*2+1;
                    }
                    sort.push_back(sort[l]*2+1-min);
                    
                    r++;
                }
                l++;
            }
            if (max-min+1>maxres) maxres = max-min+1;
            floor++;
        }
        int a= res[floor] .size();
        res.pop_back();
        return maxres;
    }
};
